package views;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JMenu;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JMenuItem;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JPanel;

import model.*;

import java.awt.Font;

import javax.swing.UIManager;
import javax.swing.SwingConstants;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.BoxLayout;

import services.ClientesServices;
import services.PaquetesServices;

import java.awt.CardLayout;
import java.awt.SystemColor;
import java.awt.GridLayout;

import net.miginfocom.swing.MigLayout;
import javax.swing.JProgressBar;

public class PanelPunto3 extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTable table;
	JProgressBar progressBar;
	
	private void EscribirArchivoPaquetes(){
		
		//########### 1 #######
		// CREAMOS ARTCHIVO PAQUETES .txt con estos datos
		
		ObjectOutputStream saveclient = null;
		try {
			saveclient = new ObjectOutputStream(new BufferedOutputStream(
					new FileOutputStream("paquetes.txt")));
				
			Paquetes p1 = new Paquetes(1,"Ignacio","Angaut","355515151","Vendedor1","Cliente1");
			Paquetes p2 = new Paquetes(2,"Otro","Apellido","355515151","Vendedor3","Cliente1");
			Paquetes p3 = new Paquetes(3,"Ignacio","Boudgouste","151511231","Vendedor2","Cliente1");
			Paquetes p4 = new Paquetes(4,"Diego","Maradona","355515151","Vendedor1","Cliente1");

			List<Paquetes> qs = Arrays.asList(p1, p2, p3, p4);
			saveclient.writeObject(qs);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {

			try {
				saveclient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	
		
	}

	private void LeerPaquetes(){
		
		
		// LEER CLIENTES
		ObjectInputStream dos = null;
		try {

			dos = new ObjectInputStream(new BufferedInputStream(
					new FileInputStream("paquetes.txt")));

			while (true) {

				try {

					List<Paquetes> PaquetesTXT = (List<Paquetes>) dos.readObject();
					//System.out.println(PaquetesTXT.size());
					// display its data
					
					progressBar.setMaximum( PaquetesTXT.size());
					int x=0;
					for (Paquetes q : PaquetesTXT) {
						x++;
						//Thread.sleep(1000 );
						progressBar.setValue(x);
						System.out.println("ProgressBar Value: " + progressBar.getValue() + "/" + progressBar.getMaximum());
					
						System.out.println("Vuelo a nombre de " + q.getNombre() + " " + q.getApellido());
						DefaultTableModel defaultModel = (DefaultTableModel) table
								.getModel();
						Vector row = new Vector();
						row.add(q.getVuelo());
						row.add(q.getNombre());
						row.add(q.getApellido());
						row.add(q.getPasaporte());
						row.add(q.getVendedor());
						row.add(q.getCliente());
					
						// table = defaultModel
					
						
						//Punto3
						//GRABAMOS EN LA BASE TAMBIEN
						Paquetes pqtemp = new Paquetes(q.getVuelo(),
								q.getNombre(),q.getApellido(),
								q.getPasaporte(),q.getVendedor(),
								q.getCliente());
								
		
						
						try {
							new PaquetesServices().SavePaquete(pqtemp);
							System.out.println("Paquete fue Cargado: " + q.getNombre() + " " + q.getApellido());
						} catch (Exception e) {
							// TODO Auto-generated catch block
							//e.printStackTrace();
							System.out.println("Paquete no Cargado: " + q.getNombre() + " " + q.getApellido());
						}
						
						defaultModel.addRow(row);
						row = null;
		
					//
					//	System.out.println("Paquete Cargado: " + q.getNombre());

					}

				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		} catch (Exception e) {

		} finally {
			System.out.println("Fin de Cargando Paquetes");
			if (dos != null) {

				try {
					dos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}

	}
	
	
	public PanelPunto3() {
		

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(SystemColor.control);
		//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		// this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		// GENERAL
		//setTitle("Asistente de Viajes - Ver Clientes");
		setSize(new Dimension(911, 605));
		//setOpacity(01);

		// FECHA
		// System.out.println(a.toString());

		// IMG
		BufferedImage myPicture;
		panel_1.setBounds(10, 11, 545, 569);
		add(panel_1);
						panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
				
						// PARTICULAR
				
						JPanel panel = new JPanel();
						
						panel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
					//	panel.setAlignmentX(CENTER_ALIGNMENT);
						panel_1.add(panel);
																				panel.setLayout(new MigLayout("", "[468.00px]", "[][38.00px][][]"));
								//panel_2.setAlignmentX(CENTER_ALIGNMENT);
								
																				JLabel lblClientesActuales = new JLabel("Paquetes Actuales");
																				lblClientesActuales.setAlignmentX(Component.CENTER_ALIGNMENT);
																				panel.add(lblClientesActuales, "cell 0 1,alignx center,aligny top");
																				lblClientesActuales.setAlignmentY(Component.TOP_ALIGNMENT);
																				lblClientesActuales.setVerticalAlignment(SwingConstants.TOP);
																				
																					lblClientesActuales.setFont(new Font("Tahoma", Font.PLAIN, 16));
		
		progressBar = new JProgressBar();
		panel.add(progressBar, "cell 0 2");

		table = new JTable();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane tableSP = new JScrollPane(table);
		tableSP.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		panel.add(tableSP, "cell 0 3,growx,aligny bottom");
		
				tableSP.setBorder(new EmptyBorder(5, 5, 5, 5));
				
						table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
								"VueloID", "Nombre", "Apellido", "Pasaporte", "Vendedor", "Cliente"}) {
							Class[] columnTypes = new Class[] { Integer.class, String.class,
									String.class , String.class, String.class, String.class};
				
							public Class getColumnClass(int columnIndex) {
								return columnTypes[columnIndex];
							}
						});
						
								table.getTableHeader().setReorderingAllowed(false);// no reordenar
								// columnas
table.setAutoResizeMode(JTable.WIDTH);// que ocupe todo lo del jpanel

EscribirArchivoPaquetes();
LeerPaquetes();
	}
	


}

	
